package org.example.task.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.task.mapper.NotificationMapper;
import org.example.task.model.dto.PageDTO;
import org.example.task.model.dto.notification.SendNotification;
import org.example.task.model.dto.notification.UpdateNotification;
import org.example.task.model.po.Notification;
import org.example.task.model.query.NotificationPageQuery;
import org.example.task.model.vo.notification.NotificationVO;
import org.example.task.service.INotificationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Notification> implements INotificationService {
    @Override
    public boolean send(SendNotification sendNotification) {
        List<Notification> notifications = new ArrayList<>();
        for (Long receiverId : sendNotification.getReceiverIds()) {
            Notification notification = new Notification();
            notification.setReceiverId(receiverId);
            notification.setContent(sendNotification.getContent());
            notification.setNotificationTime(LocalDateTime.now());
            notifications.add(notification);
        }
        return saveBatch(notifications);
    }

    @Override
    public PageDTO<NotificationVO> getList(NotificationPageQuery notificationPageQuery) {
        Page<Notification> p = notificationPageQuery.toPage();
        lambdaQuery().eq(notificationPageQuery.getStatus() != null, Notification::getStatus, notificationPageQuery.getStatus())
                .ge(notificationPageQuery.getStartTime() != null, Notification::getNotificationTime, notificationPageQuery.getStartTime())
                .le(notificationPageQuery.getEndTime() != null, Notification::getNotificationTime, notificationPageQuery.getEndTime())
                .page(p);

        return PageDTO.of(p, NotificationVO.class);
    }


}
